﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SparePartApplyConfirmReceiveReturnProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm sparePartApplyForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            serviceStationLabel.Text = sparePartApplyForm.ServiceStation.Name;
            productTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            sendDateLabel.Text = sparePartApplyForm.SendTime.HasValue ? sparePartApplyForm.SendTime.Value.ToString("yyyy-MM-dd") : "";
            receiveDateLabel.Text = sparePartApplyForm.ReceiveTime.HasValue ? sparePartApplyForm.ReceiveTime.Value.ToString("yyyy-MM-dd") : "";
            returnProductTypeLabel.Text = sparePartApplyForm.ReturnProductType;
            returnSerialNumberLabel.Text = sparePartApplyForm.ReturnProductSerialNumber;
            originalTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            applowLabel.Text = sparePartApplyForm.ApproveType == null ? sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName : sparePartApplyForm.ApproveType;
            versionLabel.Text = sparePartApplyForm.ApplicationProduct.ProductVersionNumber;
            originalTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            returnTypeLabel.Text = sparePartApplyForm.ReturnProductType;
            rmaCodeLabel.Text = sparePartApplyForm.ApplicationProduct.ApplicationForm.ApplicationCode;
            statusLabel.Text = sparePartApplyForm.Status;
        }

        protected void confirmReceiveLinkButton_Click(object sender, EventArgs e)
        {
            receiveModalPopupExtender.Show();
            returndateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");            
        }


        protected void returnLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm sparePartApplyForm = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                sparePartApplyForm.ReceiveReturnTime = Convert.ToDateTime(returndateLabel.Text);
                //添加完成日期
                sparePartApplyForm.CompleteTime = Convert.ToDateTime(returndateLabel.Text);
                sparePartApplyForm.Status = "备件-已归还";
                service.Update(sparePartApplyForm);

                Response.Write(string.Format("<script language='JavaScript'> alert( '备件已归还！');location.href= 'SparePartApplyListConfirmReturn.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplyConfirmRetrunProduct.aspx::returnLinkButton_Click");
                throw ex;
            }
        }

        protected void repairLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm sparePartApplyForm = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                sparePartApplyForm.ReceiveReturnTime = Convert.ToDateTime(returndateLabel.Text);
                sparePartApplyForm.Status = "备件-已归还";
                service.Update(sparePartApplyForm);

                ApplicationService appService = new ApplicationService();
                UserService userservice = new UserService();
                WXYW.DAL.User userentity = userservice.GetUserByName(System.Configuration.ConfigurationManager.AppSettings["BJKF"].ToString());
                ApplicationForm newForm = new ApplicationForm();
                newForm.ApplicationDateTime = DateTime.Now;
                newForm.ApplicationType = "R";
                newForm.User = userentity;
                newForm.IsSubmit = true;
                newForm.Status = "等待RMA单号确认";
                newForm.RunningNumber = RunningNumberService.GenerateApplicationNumber();
                if (!sparePartApplyForm.ApplicationProductReference.IsLoaded)
                {
                    sparePartApplyForm.ApplicationProductReference.Load();
                }
                if (!sparePartApplyForm.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    sparePartApplyForm.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!sparePartApplyForm.ApplicationProduct.ProblemTypeReference.IsLoaded)
                {
                    sparePartApplyForm.ApplicationProduct.ProblemTypeReference.Load();
                }
                if (!sparePartApplyForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    sparePartApplyForm.ApplicationProduct.ApplicationFormReference.Load();
                }
                newForm.ReleventRMANO = sparePartApplyForm.ApplicationProduct.ApplicationForm.ApplicationCode;

                ApplicationProduct product = new ApplicationProduct
                {
                    ProductType = sparePartApplyForm.ApplicationProduct.ProductType,
                    ProductCount = 1,
                    ProductSerialNumber = sparePartApplyForm.ApplicationProduct.ProductSerialNumber,
                    ProductVersionNumber = sparePartApplyForm.ApplicationProduct.ProductVersionNumber,
                    ProblemType = sparePartApplyForm.ApplicationProduct.ProblemType,
                    ProductProblemDescription = sparePartApplyForm.ApplicationProduct.ProductProblemDescription,
                    ProductAppearance = sparePartApplyForm.ApplicationProduct.ProductAppearance,
                    ProductPurchaseDateTime = sparePartApplyForm.ApplicationProduct.ProductPurchaseDateTime,
                    ProductRepairType = "保内"
                };

                newForm.ApplicationProduct.Add(product);

                long applicationID = appService.AddApplication(newForm);

                appService.SubmitApplication(applicationID);

                Response.Write(string.Format("<script language='JavaScript'> alert( '备件已生成维修申请单，流水号为:" + newForm.RunningNumber + "');location.href= 'SparePartApplyListConfirmReturn.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplyConfirmRetrunProduct.aspx::repairLinkButton_Click");
                throw ex;
            }
        }
    }
}
